import { Toast } from "vant";
import { scrollMixin } from "../../../utils/mixin";
import { Image as VanImage } from "vant";
import yzWechatShareImg from "components/ui_components/yz_wechatShareImg";
import cTitle from "components/title";
export default {
  mixins: [scrollMixin], //加载更多
  components: {
    VanImage,
    cTitle,
    yzWechatShareImg
  },
  data() {
    return {
      cstoreId: null,
      commentType: 0,
      commentName: "评价类型",
      commentTypeShow: false,
      commentTypeArr: [
        {
          name: "推荐",
          value: 3
        },
        {
          name: "一般",
          value: 2
        },
        {
          name: "不推荐",
          value: 1
        }
      ],
      isLoadMore: true,
      page: 1,
      totalPage: 0,
      commentList: []
    };
  },
  activated() {
    if ((this.$route.query.storeId ?? "") !== "") {
      this.cstoreId = Number(this.$route.query.storeId);
      this.storeGoodsList();
    }
  },
  methods: {
    assessmentType() {
      this.commentTypeShow = !this.commentTypeShow;
    },
    commentTypeOn(value, name) {
      this.commentType = value;
      this.commentName = name;
      this.commentTypeShow = false;
      this.storeGoodsList();
    },
    storeGoodsList() {
      //搜索商户
      let json = {
        cstore_id: this.cstoreId,
        page: this.page,
        comment_type: this.commentType
      };
      $http.post("plugin.coupon-store.frontend.store-manage.commentList", json, "正在获取").then(
        response => {
          if (response.result == 1) {
            let res = response.data;
            this.isLoadMore = true;
            this.totalPage = res.last_page;
            if (!this.totalPage) {
              this.page = 0;
            }
            this.commentList = res.data;
          } else {
            this.page = this.page - 1;
            this.isLoadMore = false;
            this.$dialog.alert({ message: response.msg });
          }
        },
        function(response) {
          // console.log(response);
          // error callback
        }
      );
    },
    getMoreData() {
      this.isLoadMore = false; // 防止多次请求分页数据
      if (this.page >= this.totalPage) {
        return;
      } else {
        this.page = this.page + 1;
        let json = {
          store_id: this.cstoreId,
          page: this.page
        };
        $http.post("plugin.coupon-store.frontend.store-manage.commentList", json, "正在获取").then(
          response => {
            if (response.result == 1) {
              let res = response.data;
              let list = res.data;
              this.isLoadMore = true;
              this.commentList = [...this.commentList, ...list];
            } else {
              this.page = this.page - 1;
              this.isLoadMore = false;
              this.$dialog.alert({ message: response.msg });
            }
          },
          function(response) {
            // console.log(response);
            // error callback
          }
        );
      }
    },
    handleScroll() {
      //滚动事件触发
      if (this.getScrollTop() + this.getClientHeight() + 105 > this.getScrollHeight()) {
        //此处发起请求
        if (this.isLoadMore) {
          this.getMoreData();
        } else {
          console.log("没有更多数据");
        }
      }
    }
  }
};
